package com.store.orm.mongo.curd.impl;

import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
import com.store.orm.mongo.curd.Insert;
import com.store.orm.mongo.curd.MongoUtil;
import org.bson.Document;

import java.util.*;

/**
 * @author 宫静雨
 * @version 1.0
 */
public class MongoInsert implements Insert {
    private MongoDatabase database;
    private static final MongoUtil util = new MongoUtil();

    public MongoInsert(MongoDatabase database) {
        this.database = database;
    }

    @Override
    public <T> void insert(T t) {
        try {
            MongoCollection<Document> collection = database.getCollection(util.collection(t.getClass()));
            Document document = util.parseTToDoc(t);
            collection.insertOne(document);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public <T> void insertMany(List<T> ts) {
        if (ts.size() == 0) {
            return;
        }
        try {
            MongoCollection<Document> collection = database.getCollection(util.collection(ts.get(0).getClass()));
            ArrayList<Document> documents = new ArrayList<>();
            for (T t : ts) {
                Document document = util.parseTToDoc(t);
                documents.add(document);
            }
            collection.insertMany(documents);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insert(Class<?> cls, String json) {
        MongoCollection<Document> collection = database.getCollection(util.collection(cls));
        try {
            collection.insertOne(util.parseJsonToDoc(json));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insertMany(Class<?> cls, List<String> jsons) {
        if (jsons.size() == 0) {
            return;
        }
        try {
            MongoCollection<Document> collection = database.getCollection(util.collection(cls));
            ArrayList<Document> documents = new ArrayList<>();
            for (String json : jsons) {
                documents.add(util.parseJsonToDoc(json));
            }
            collection.insertMany(documents);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void insert(Class<?> cls, LinkedHashMap<String, Object> map) {
        MongoCollection<Document> collection = database.getCollection(util.collection(cls));
        Document document = util.parseMapToDocument(map);
        collection.insertOne(document);
    }

    @Override
    public void insertMany(Class<?> cls, Collection<LinkedHashMap<String, Object>> maps) {
        if (maps.size() == 0) {
            return;
        }
        MongoCollection<Document> collection = database.getCollection(util.collection(cls));
        ArrayList<Document> documents = new ArrayList<>();
        for (LinkedHashMap<String, Object> map : maps) {
            documents.add(util.parseMapToDocument(map));
        }
        collection.insertMany(documents);
    }

    @Override
    public void insert(Class<?> cls, Document document) {
        MongoCollection<Document> collection = database.getCollection(util.collection(cls));
        collection.insertOne(document);
    }

    @Override
    public void insertMany(Class<?> cls, Document[] list) {
        if (list.length == 0) {
            return;
        }
        List<Document> documents = new ArrayList<>(Arrays.asList(list));
        MongoCollection<Document> collection = database.getCollection(util.collection(cls));
        collection.insertMany(documents);
    }
}